package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {


    @Autowired
    private UserInfoMapper userInfoMapper;

    /*用户资料 - 保存(新增)*/
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /*用户资料 - 更新*/
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /*用户资料 - 读取*/
    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    /*用户资料 - 批量读取*/
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, UserInfo userInfo) {
        QueryWrapper qw = new QueryWrapper();
        qw.in("id", ids);
        if (userInfo != null) {
            if (userInfo.getAge() != null) {
                qw.lt("age", userInfo.getAge());
            }
            if (!StringUtils.isEmpty(userInfo.getGender())) {
                qw.eq("gender", userInfo.getGender());
            }
        }
        List list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }
}